
//
//  AddressManagerTableViewCell.m
//  ChunLaiZhuangClient
//
//  Created by xielihui on 16/3/16.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "AddressManagerTableViewCell.h"
#import "AddressListModel.h"

@interface AddressManagerTableViewCell()
@property (weak, nonatomic) IBOutlet UIButton *setDefaultBtn;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (nonatomic, strong) AddressSubModel *addressModel;

@end

@implementation AddressManagerTableViewCell

- (void)awakeFromNib {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    // Initialization code
}

- (void)updateAddressInfo:(AddressSubModel *)model {
    self.nameLabel.text = model.firstname;
    self.phoneLabel.text = model.telephone;
    self.addressLabel.text = model.address;
    self.addressLabel.preferredMaxLayoutWidth = SCREEN_WIDTH;
    self.addressModel = model;
    [self.addressModel addObserver:self forKeyPath:@"is_default" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
    if (model.is_default) {
        [self.setDefaultBtn setImage:[UIImage imageNamed:@"btn_select_selectd"] forState:UIControlStateNormal];
    } else {
         [self.setDefaultBtn setImage:[UIImage imageNamed:@"btn_select_nor"] forState:UIControlStateNormal];
    }
}

- (IBAction)setDefualtAddressAction:(id)sender {
    if (self.setDefaultAddressAction) {
        self.setDefaultAddressAction();
    }
}

- (IBAction)deleteAddressAction:(id)sender {
    if (self.deleteAddressAction) {
        self.deleteAddressAction();
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"is_default"]) {
        if (self.addressModel.is_default) {
            [self.setDefaultBtn setImage:[UIImage imageNamed:@"btn_select_selectd"] forState:UIControlStateNormal];
        } else {
            [self.setDefaultBtn setImage:[UIImage imageNamed:@"btn_select_nor"] forState:UIControlStateNormal];
        }
    }
}

- (void)dealloc {
    [self.addressModel removeObserver:self forKeyPath:@"is_default"];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
